<template>
  <div class="login">
    <el-form
      ref="forgetPwdForm"
      :model="forgetPwdForm"
      :rules="forgetPwdRules"
      label-width="100px"
      class="forgetPwd-form"
    >
      <h3 class="title">忘记密码</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="forgetPwdForm.username" placeholder="请输入用户名" type="text" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="forgetPwdForm.newPassword" placeholder="请输入新密码" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="forgetPwdForm.confirmPassword" placeholder="请确认密码" type="password" />
      </el-form-item>
      <el-form-item label="短信验证码" prop="smsCode">
        <el-input
          v-model="forgetPwdForm.smsCode"
          placeholder="请输入短信验证码"
          size="mini"
          maxlength="6"
          style="width: 40%"
        />
        <div style="width: 50%; float: right">
          <div class="log_getcode log_getcode113" v-if="showCode" @click="getSmsCode">
            <el-button type="primary">获取验证码</el-button>
          </div>
          <div class="log_getcode log_getcode113" v-else>
            <el-button type="primary" disabled>还剩 {{ timeout }} 秒</el-button>
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" size="mini" type="primary" @click.native.prevent="handleConfirm">
          <span>确 定</span>
        </el-button>
        <el-button :loading="loading" size="mini" type="primary" @click.native.prevent="handleCancel">
          <span>取 消</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span
        >版权所有(C) 清华大学苏州汽车研究院(吴江) | 苏ICP备<a href="http://www.beian.miit.gov.cn">11076804-1号</a></span
      >
    </div>
  </div>
</template>

<script>
import { sendSmsCode, forgetPwd } from '@/api/system/user'
import { getCodeImg } from '@/api/login'

export default {
  name: 'ForgetPwd',
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.forgetPwdForm.newPassword !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      showCode: true,
      timeout: 60,
      forgetPwdForm: {
        username: '',
        newPassword: '',
        confirmPassword: '',
        code: '',
        uuid: '',
      },
      // 表单校验
      forgetPwdRules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        newPassword: [
          { required: true, message: '新密码不能为空', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
        ],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { required: true, validator: equalToPassword, trigger: 'blur' },
        ],
      },
      loading: false,
      redirect: undefined,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    //动态验证码
    getSmsCode() {
      this.$refs.forgetPwdForm.validate((valid) => {
        if (valid) {
          this.showCode = false
          var setInter = setInterval(() => {
            if (this.timeout > 1) {
              this.timeout--
            } else {
              this.timeout = 60
              clearInterval(setInter)
              this.showCode = true
            }
          }, 1000)
          sendSmsCode('', this.forgetPwdForm.username, 5)
            .then((response) => {
              this.msgSuccess(response.msg)
              this.forgetPwdForm.uuid = response.uuid
            })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },
    handleConfirm() {
      this.$refs.forgetPwdForm.validate((valid) => {
        if (valid) {
          forgetPwd(
            this.forgetPwdForm.username,
            this.forgetPwdForm.newPassword,
            this.forgetPwdForm.smsCode,
            this.forgetPwdForm.uuid
          )
            .then((response) => {
              this.msgSuccess(response.msg)
              this.$router.push('/login')
            })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },
    handleCancel() {
      this.$router.push('/login')
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('../assets/image/login-background.jpg');
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.forgetPwd-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
